/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wenming.notify.slice;

import com.wenming.notify.NotifyPageProvider;
import com.wenming.notify.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.PageSlider;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.window.service.DisplayManager;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener,
        NotifyPageProvider.InterceptionListener {
    private Text mNoText;
    private Text mMeText;
    private DependentLayout mMeLayout;
    private DependentLayout mNoLayout;

    private PageSlider mPageSlider;
    private DependentLayout mLineAll;

    private int mWidth;
    private NotifyPageProvider mNotifyPageProvider;
    private int mWid;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initView();
        initListener();
    }

    private void initView() {
        mMeLayout = (DependentLayout) findComponentById(ResourceTable.Id_me_layout);
        mNoLayout = (DependentLayout) findComponentById(ResourceTable.Id_no_layout);
        mNoText = (Text) findComponentById(ResourceTable.Id_no_text);
        mNoText.setTextColor(Color.WHITE);
        mMeText = (Text) findComponentById(ResourceTable.Id_me_text);
        mMeText.setTextColor(new Color(Color.getIntColor("#D3D3D3")));

        mWidth = DisplayManager.getInstance().getDefaultDisplay(getContext()).get().getRealAttributes().width;
        mWid = mWidth / 2;
        mLineAll = (DependentLayout) findComponentById(ResourceTable.Id_me_line_all);
        mLineAll.setWidth(mWid);

        mPageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        mNotifyPageProvider = new NotifyPageProvider(this);
        mPageSlider.setProvider(mNotifyPageProvider);
    }

    private void initListener() {
        mMeLayout.setClickedListener(this);
        mNoLayout.setClickedListener(this);

        mNotifyPageProvider.setInterceptionListener(this);

        mPageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
                if (mLineAll.getTranslationX() < mWid && i1 > 0){
                    mLineAll.setTranslationX(i1);
                } else if ((mLineAll.getTranslationX() >= mWid && i1 > 0)){
                    mLineAll.setTranslationX(mWid);
                } else if (mLineAll.getTranslationX() > 0 && i1 < 0){
                    mLineAll.setTranslationX(mWid + i1);
                } else if (mLineAll.getTranslationX() <= 0 && i1 < 0){
                    mLineAll.setTranslationX(0);
                }
            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {
                if (i == 0){
                    mNoText.setTextColor(Color.WHITE);
                    mMeText.setTextColor(new Color(Color.getIntColor("#D3D3D3")));
                    mLineAll.setTranslationX(0);

                    if (mPageSlider.getCurrentPage() == 1) {
                        mPageSlider.setCurrentPage(0);
                    }
                } else if (i == 1){
                    mMeText.setTextColor(Color.WHITE);
                    mNoText.setTextColor(new Color(Color.getIntColor("#D3D3D3")));
                    mLineAll.setTranslationX((float) mWid);

                    if (mPageSlider.getCurrentPage() == 0) {
                        mPageSlider.setCurrentPage(1);
                    }
                }
            }
        });
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_me_layout:
                mMeText.setTextColor(Color.WHITE);
                mNoText.setTextColor(new Color(Color.getIntColor("#D3D3D3")));
                mLineAll.setTranslationX((float)mWid);

                if (mPageSlider.getCurrentPage() == 0) {
                    mPageSlider.setCurrentPage(1);
                }
                break;
            case ResourceTable.Id_no_layout:
                mNoText.setTextColor(Color.WHITE);
                mMeText.setTextColor(new Color(Color.getIntColor("#D3D3D3")));
                mLineAll.setTranslationX(0);

                if (mPageSlider.getCurrentPage() == 1) {
                    mPageSlider.setCurrentPage(0);
                }
                break;
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void interceptPageSlider(boolean isSlide) {
        if (isSlide == mPageSlider.isEnabled()) {
            return;
        }
        mPageSlider.setEnabled(isSlide);
    }
}
